﻿"use strict"
const mvc = require("cosjs.mvc");
exports.index = function () {
    let api = this.get("api")
    let sid = this.get("sid", "int")
    if (!api) {
        return this.error("api empty")
    }
    let branch = this.branch;
    let data = { api: api, sid: sid, handle: "branch/proxy", branch: branch, "server": {} };
    this.view = "branch/proxy";

    let keys = ["name"]
    let server = this.model.branch("server", branch);
    return server.mget(keys).then(ret => {
        for (let k in ret) {
            let v = ret[k];
            let sid = v._id;
            if (!data["sid"]) {
                data["sid"] = sid;
            }
            data["server"][sid] = v.name;
        }
        return data;
    })
}

exports.page = function () {
    this.output = 'json';
    let sid = this.get("sid", "int");
    let api = this.get("api");
    if (!sid || !api) {
        return this.error("args empty");
    }
    let branch = this.branch;
    return this.library("game/request", branch, sid, api, this.req.body);
}

exports.save = function () {
    this.output = 'json';
    let sid = this.get("sid", "int");
    let api = this.get("api");
    if (!sid || !api) {
        return this.error("args empty");
    }
    let branch = this.branch;
    return this.library("game/request", branch, sid, api, this.req.body);
}


//get game config
exports.config = function () {
    this.output = 'js';
    let key = this.get("key", "string");
    let arr = key.split(",");
    let config = [];
    let data = [`if(!cosjs) {var cosjs = {}};`];
    data.push(`if(!cosjs['config']) { cosjs['config'] = {} };`);
    for (let k of arr) {
        if (mvc.config(k)) {
            let doc = mvc.config(k);
            config.push(`cosjs['config']['${k}'] = {}`);
            for (let i in doc) {
                if (k != "itemGroup") {
                    let name = doc[i].itemName_translate_cn;
                    config.push(`cosjs.config['${k}']["${i}"] ="${name}"`);
                } else {
                    let name = doc[i].Coll[0].itemName_translate_cn;
                    config.push(`cosjs.config['${k}']["${i}"] ="${name}"`);
                }


                // let des = null;
                // for (let i = 9; i >= 1; --i) {
                //     des = mvc.config('MsgList' + i, name);
                //     if (des) {
                //         break;
                //     }
                // }
                // if (des) {
                //     if (des.cn) {
                //         console.log("des.cn:", des.cn);
                //         config.push(`cosjs.config['${k}']["${i}"] ="${des.cn}"`);
                //     } else {
                //         console.log("des:", des);
                //         config.push(`cosjs.config['${k}']["${i}"] ="${des}"`);
                //     }
                // } else {
                //     console.log("i:", i);
                // }
            }
        }
        // if(k=="MsgList")
        // {
        //     let k1="MsgList9";
        //     config.push(`cosjs.${type}["${k1}"] =`+JSON.stringify(mvc.config(k1)));

        // }
        // else
        // {
        //     if(!mvc.config(k)){
        //           data.push(`/*game.${type}[${k}] not exist*/`);
        //     }
        //     else{
        //          config.push(`cosjs.${type}["${k}"] =`+JSON.stringify(mvc.config(k)));
        //     }
        // }
    }
    let result = data.concat(config);
    //console.log(result);
    return result.join("\r\n");
    //  return mvc.config()
    //    return game_config_start.call(this,"config")
}
//get game format
exports.format = function () {
    return game_config_start.call(this, "format")
}




function game_config_start(type) {
    let key = this.get("key", "string");
    let branch = this.branch;
    if (!key) {
        this.status = 404;
        return this.error("key empty");
    }
    this.output = 'js';
    let api = type === 'config' ? '/config/' : '/config/format';
    let query = { "key": key };
    let branchModel = this.model.admin("branch");
    return branchModel.get(branch, ['gcid', 'secret']).then(ret => {
        if (!ret['gcid']) {
            console.log("----------------------------------1")
            this.status = 404;
            return this.error(`branch gcid empty`);
        }
        else {
            let sid = parseInt(ret["gcid"]);
            console.log("----------------------------------2")
            return this.library("game/request", ret, sid, api, query);
        }
    }).then(body => {

        let ret = body || {};
        let arr = key.split(",");
        let config = [];
        let data = [`if(!cosjs) {var cosjs = {}};`];
        data.push(`if(!cosjs['${type}']) { cosjs['${type}'] = {} };`);
        for (let k of arr) {
            if (k == "MsgList") {
                let k1 = "MsgList9";
                config.push(`cosjs.${type}["${k1}"] =` + JSON.stringify(ret[k1]))

            }
            else {
                if (!ret[k]) {
                    data.push(`/*game.${type}[${k}] not exist*/`);
                }
                else {
                    config.push(`cosjs.${type}["${k}"] =` + JSON.stringify(ret[k]))
                }
            }
        }
        let result = data.concat(config);
        return result.join("\r\n");
    })
}
